package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.constant.RedisPermissionConstant;
import com.dk.mapper.PersonRoleMapper;
import com.dk.mapper.TPersonMapper;
import com.dk.mapper.TRoleMapper;
import com.dk.pojo.PersonRoleExample;
import com.dk.pojo.TPerson;
import com.dk.pojo.TPersonExample;
import com.dk.pojo.TRole;
import com.dk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

@Service
@org.springframework.stereotype.Service //spring实例化对象的注解
public class UserServiceImpl implements UserService {
    @Autowired
    private TPersonMapper tPersonMapper;
    @Autowired
    private TRoleMapper roleMapper;
    @Autowired
    private PersonRoleMapper personRoleMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TPerson> getInfo() {
        return tPersonMapper.selectByExample(null);
    }

    @Override
    public List<TRole> getRoleInfo() {
        return roleMapper.selectByExample(null);
    }
    //新增
    @Override
    public boolean addInfo(TPerson person, Integer[] rids) {
        //增加用户表 返回主键
        int i = tPersonMapper.insertSelective(person);
        //增加中间表
        personRoleMapper.insertBatch(person.getPid(),rids);
        return true;
    }

    @Override
    public TPerson getUserInfoById(Integer pid) {
        return tPersonMapper.selectByPrimaryKey(pid);
    }

    @Override
    public List<Integer> getUserRoleInfoById(Integer pid) {
        return personRoleMapper.getUserRoleInfoById(pid);
    }
    //修改
    @Override
    public boolean updateInfo(TPerson person, Integer[] rids) {
        int i = tPersonMapper.updateByPrimaryKeySelective(person);
        //删除中间表信息 pid
        PersonRoleExample pr = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = pr.createCriteria();
        criteria.andIdEqualTo(person.getPid());
        personRoleMapper.deleteByExample(pr);
        //增加中间表
        personRoleMapper.insertBatch(person.getPid(),rids);
        //同步redis 删除redis中的缓存数据
        redisTemplate.opsForHash().delete(RedisPermissionConstant.MENU_INFO_KEY,RedisPermissionConstant.MENU_INFO_FIELD + person.getPid());
        return false;
    }
    //删除
    @Override
    public boolean deleteUserInfo(Integer pid) {
        tPersonMapper.deleteByPrimaryKey(pid);
        //删除中间表信息 pid
        PersonRoleExample pr = new PersonRoleExample();
        PersonRoleExample.Criteria criteria = pr.createCriteria();
        criteria.andIdEqualTo(pid);
        personRoleMapper.deleteByExample(pr);
        redisTemplate.opsForHash().delete(RedisPermissionConstant.MENU_INFO_KEY,RedisPermissionConstant.MENU_INFO_FIELD + pid);
        return false;
    }

    @Override
    public TPerson selectByName(String pname) {
        TPersonExample te = new TPersonExample();
        TPersonExample.Criteria criteria = te.createCriteria();
        criteria.andPnameEqualTo(pname);
        List<TPerson> list = tPersonMapper.selectByExample(te);
        return list.size() > 0 ? list.get(0) : null;
    }
}
